package com.org.fileUpload;

import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class RemoveFile {

    private final FileStorageService fileStorageService;//注入实列

    public void deleteFile(List<String> filePaths){
        System.out.println("开始删除文件");
        for(String filePath : filePaths){
            //例：https://memoirs-objects.oss-cn-guangzhou.aliyuncs.com/memoirsData/2025/11/01/6905a1394850351156b7e281.jpg，从第四个斜杠开始截取
            String[] parts = filePath.split("/", 8);
            // 获取基础路径
            String basePath = parts[3] + "/";
            // 获取自定义路径
            String leftPart = parts[4] + "/" + parts[5] + "/" + parts[6] + "/";
            // 文件名称
            String fileName = parts[7];

            // 创建FileInfo对象
            FileInfo fileInfo = new FileInfo()
                    .setPlatform("aliyun-oss-1")
                    .setBasePath(basePath)
                    .setPath(leftPart)
                    .setFilename(fileName);

            // 删除文件
            fileStorageService.delete(fileInfo);
        }
    }

}
